package learnMethod

import (
	"encoding/json"
	"fmt"
)

type Cat struct {
	name  string
	age   int
	color string
}

func CatTest() {
	CheckCat("tom")
}
func CheckCat(name string) {
	isCat := make(map[string]Cat, 2)
	Cat01 := Cat{name: "tom", age: 22, color: "蓝色"}
	Cat02 := Cat{name: "lisi", age: 23, color: "红色"}
	isCat["tom"] = Cat01
	isCat["lisi"] = Cat02

	var Cat03 *Cat = &Cat01
	fmt.Println((*Cat03).name)
	fmt.Println(Cat03.name)
	fmt.Printf("%p\n", &Cat01)
	fmt.Println(&Cat03)
	fmt.Printf("Cat01.name的地址为%p\n", &(Cat01.name))
	fmt.Printf("Cat03.name的地址为%p\n", &(Cat03.name))
	fmt.Printf("Cat03.age的地址为%p\n", &(Cat03.age))
	fmt.Printf("Cat03.color的地址为%p\n", &(Cat03.color))
	/*if !reflect.DeepEqual(isCat[name], Cat{}) {
		fmt.Printf("小猫名字=%v,年龄=%v,颜色=%v\n", isCat[name].name, isCat[name].age, isCat[name].color)
	} else {
		fmt.Println("没有这只猫猫")
	}

	for k1, v1 := range isCat {
		fmt.Println(k1)
		fmt.Printf("小猫名字=%v,年龄=%v,颜色=%v\n", v1.name, v1.age, v1.color)
	}*/

}

type point struct {
	x int
	y int
}
type point02 struct {
	x int
	y int
}

type rect struct {
	leftUp, rightDown point
}

type rect2 struct {
	leftUp, rightDown *point
}

func StruceTest01() {
	/*	r1 := rect{point{1, 2}, point{1, 2}}
		fmt.Printf("r1的leftUp的x地址=%v,r1的leftUp的y地址=%v,"+
			"r1的rightDown的x地址=%v,r1的rightDown的y地址=%v\n",
			&r1.leftUp.x, &r1.leftUp.y, &r1.rightDown.y, &r1.rightDown.y)

		r2 := rect2{&point{1, 2}, &point{1, 2}}
		fmt.Printf("r2的leftUp的x地址=%v,r2的leftUp的y地址=%v,"+
			"r2的rightDown的x地址=%v,r2的rightDown的y地址=%v\n",
			&r2.leftUp.x, &r1.leftUp.y, &r1.rightDown.y, &r1.rightDown.y)

		one := point{1, 2}
		two := point02(one)
		fmt.Println("two=", two)*/

	one := monster{"lisi", 22, "湖北武汉"}
	two := monster{"zhangsan", 22, "湖北武汉"}
	jsonStr, err := json.Marshal(one)
	if err != nil {
		fmt.Println("json处理错误", err)
	}
	fmt.Println("jsonStr=", jsonStr)

	fmt.Printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", 123, 34, 110, 97, 109, 101, 34, 58, 34, 108, 105, 115, 105, 34)

	test := one.getName()
	test02 := two.getName()
	fmt.Println(test)
	fmt.Println(test02)

	/*	one.forEach()
		two.forEach()
		one.jisuan()*/
	one.jisuanN(1000)
}

type monster struct {
	Name    string `json:"name"`
	Age     int    `json:"age"`
	Address string `json:"address"`
}

func (c monster) getName() string {
	return c.Name
}
func (c monster) forEach() {
	fmt.Printf("name=%v,age=%v,address=%v\n", c.Name, c.Age, c.Address)
}

func (c monster) jisuan() {
	var res int
	/*	res = (1000 + 1) * 1000 / 2
		fmt.Println("res=", res)*/

	for i := 1; i <= 1000; i++ {
		res += i
	}
	fmt.Println("res=", res)
}

func (c monster) jisuanN(n int) {
	var res int
	for i := 1; i <= n; i++ {
		res += i
	}
	fmt.Println("res=", res)

}

type circle struct {
	p      float64
	radius float64
}

func (c *circle) area() float64 {
	var res float64
	c.radius = 4
	res = c.p * c.radius * c.radius
	return res
}

func CircleTest() {
	cir := circle{3.14, 2}
	area := cir.area()
	fmt.Printf("area=%v\n", area)
	fmt.Println(&cir)
}

func (c *circle) String() string {
	cirStr := fmt.Sprintf("p=%v,radius=%v", c.p, c.radius)
	return cirStr
}

type MethodUtils struct {
	len int
	wid int
}

func (m MethodUtils) printRectangle() {
	len := m.len
	wid := m.wid
	for i := 0; i < len; i++ {
		for j := 0; j < wid; j++ {
			fmt.Print("*   ")
		}
		fmt.Println()
	}

}

func MethodUtilTest() {
	one := MethodUtils{10, 8}
	one.printRectangle()
}

type learnPrint struct {
	nums []int
}

func (l *learnPrint) printTest() {
	nums := l.nums
	fmt.Printf("%p\n", l)
	fmt.Println(nums)
	for i := 0; i < len(nums); i++ {
		for j := 0; j <= i; j++ {
			fmt.Printf("%v * %v =%v\t", nums[i], nums[j], nums[i]*nums[j])
		}
		fmt.Println()
	}
}

func LearnPrintTest() {
	var lp learnPrint
	one := make([]int, 9)

	for i := range one {
		fmt.Printf("请为第%v位数字赋值\n", i+1)
		fmt.Scan(&one[i])
	}
	fmt.Println(one)
	lp.nums = one
	//lp.printTest()
	(&lp).printTest()
}

type student struct {
	Name  string
	Score float64
	One   map[string]Cat
	Slice []int
}

func NewStudent(n string, s float64, one map[string]Cat, slice []int) *student {
	return &student{
		Name:  n,
		Score: s,
		One:   one,
		Slice: slice,
	}
}

func (s student) String() string {
	sprintf := fmt.Sprintf("name=%v,score=%v,cat=%v,slice=%v\n", s.Name, s.Score, s.One, s.Slice)
	return sprintf
}

func NewCat() *Cat {
	return &Cat{"tom", 22, "红色"}
}

type person struct {
	name string
	age  int
}

func (p person) String() string {
	pStr := fmt.Sprintf("name=%v,age=%v\n", p.GetName(), p.GetAge())
	return pStr
}

func (p *person) GetAge() int {
	return p.age
}
func (p *person) SetAge(age int) {
	p.age = age
}
func (p *person) GetName() string {
	return p.name
}
func (p *person) SetName(name string) {
	p.name = name
}

func PersonTest() {
	var p person
	p.SetName("zhangsan")
	p.SetAge(99)
	fmt.Println(&p)
}

func NewPerson() *person {
	var p person
	return &p
}
